import Vue from 'vue'
import Vuex from 'vuex'
import router,{resetRouter} from "@/router";
import createPersistedState from 'vuex-persistedstate'
Vue.use(Vuex) // 注册 Vuex

function addNewRoute(menuList){
    console.log(menuList)
    let routes = router.options.routes
    console.log(routes)
    routes.forEach(routeItem=>{
        if (routeItem.path=="/indexView"){
            menuList.forEach(menu=>{
                let childRoute = {
                    path:'/'+menu.menuclick,
                    name:menu.menuname,
                    meta:{
                        title:'menu.menuname'
                    },
                    component:()=>import('../components/admin/AdminManage.vue')
                }
                routeItem.children.push(childRoute)
            })
        }
    })
    resetRouter()
    router.addRoutes(routes)
}

export default new Vuex.Store({
    state: {
        menu: [] // 存储菜单数据（与 mutation "setMenu" 对应）
    },
    mutations: {
        setMenu(state,menuList) {
            state.menu = menuList
            //添加路由
            addNewRoute(menuList)
        }
    },
    getters: {
        getMenu(state) {
            return state.menu
        }
    },plugins:[createPersistedState()]
})